from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from myapp.ser import *


# Create your views here.


class LoginM(APIView):
    def post(self, request):
        ser = LoginSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20010})


class BillM(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            data = Bill.objects.all()
            ser = BillSer(data, many=True)
            return Response(ser.data)
        else:
            data = Bill.objects.get(pk=id)
            ser = BillSer(data)
            return Response(ser.data)

    def post(self, request):
        ser = BillSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20011})

    def put(self, request):
        id = request.GET.get('id')
        data = Bill.objects.get(pk=id)
        ser = BillSer(instance=data, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20015})

    def delete(self, request):
        id = request.GET.get('id')
        Bill.objects.get(pk=id).delete()
        return Response({'code': 200})


class ClerkM(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            data = Clerk.objects.all()
            ser = ClerkSer(data, many=True)
            return Response(ser.data)
        else:
            data = Clerk.objects.get(pk=id)
            ser = ClerkSer(data)
            return Response(ser.data)

    def post(self, request):
        ser = ClerkSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20012})

    def put(self, request):
        id = request.GET.get('id')
        data = Clerk.objects.get(pk=id)
        ser = ClerkSer(instance=data, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20016})

    def delete(self, request):
        id = request.GET.get('id')
        Clerk.objects.get(pk=id).delete()
        return Response({'code': 200})


class RegionM(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            data = Region.objects.all()
            ser = RegionSer(data, many=True)
            return Response(ser.data)
        else:
            data = Region.objects.get(pk=id)
            ser = RegionSer(data)
            return Response(ser.data)

    def post(self, request):
        ser = RegionSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20013})

    def put(self, request):
        id = request.GET.get('id')
        data = Region.objects.get(pk=id)
        ser = RegionSer(instance=data, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20017})

    def delete(self, request):
        id = request.GET.get('id')
        Region.objects.get(pk=id).delete()
        return Response({'code': 200})


class ClientM(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            data = Client.objects.all()
            ser = ClientSer(data, many=True)
            return Response(ser.data)
        else:
            data = Client.objects.get(pk=id)
            ser = ClientSer(data)
            return Response(ser.data)

    def post(self, request):
        ser = ClientSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20014})

    def put(self, request):
        id = request.GET.get('id')
        data = Client.objects.get(pk=id)
        ser = ClientSer(instance=data, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            return Response({'code': 20018})

    def delete(self, request):
        id = request.GET.get('id')
        Client.objects.get(pk=id).delete()
        return Response({'code': 200})
